package org.dragonnova.iot.business.cloud.transport.model;

/**
 * * 应答包帧头格式： 
 * ****************************** * 
 * ** header 1 byte (头部) 
 * ** mt 1 byte (应答码)
 * ** end  
 */

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

import org.dragonnova.iot.business.cloud.transport.ParseException;
import org.dragonnova.iot.business.cloud.transport.TooShortPackageException;
import org.dragonnova.iot.business.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * @description: 应答签消息包
 * @author songxy DateTime 2017年1月20日 下午3:45:55
 * @company winter
 * @email thinkdata@163.com
 * @version 1.0
 */
public final class AckPacket extends Packet {
	//默认包名功能码
	public static final byte ACK_TYPE_CODE_UNSUPPORTED_OPERATION = (byte) 0x90;
	//注册回传功能码
	public static final byte ACK_TYPE_CODE_REGISTER = (byte) 0x81;
	//注销回传功能码
	public static final byte ACK_TYPE_CODE_LOGOFF = (byte) 0x82;
	
	
	
	
	private final static Logger LOGGER = LoggerFactory
			.getLogger(AckPacket.class);
	private final static short PACK_PUBLIC_LEN = 4;
	
	public AckPacket() {
		this(ACK_TYPE_CODE_UNSUPPORTED_OPERATION);
	}

	public AckPacket(byte code) {
		this.mt = code;
	}

	
	public byte getAckCode() {
		return this.mt;
	}

	@Override
	public void decode(ByteBuf in) throws Exception {
		try {
			if (LOGGER.isDebugEnabled()) {
				LOGGER.debug("Receive Ack Code: [\'" + this.mt
						+ "\'] Packet.");
			}
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public ByteBuf encode() {
		try {
			
			final ByteBuf buffer = Unpooled.directBuffer(PACK_PUBLIC_LEN+Long.BYTES+Short.BYTES);
			buffer.writeByte(Packet.HEADER);
			buffer.writeByte(this.mt);
			buffer.writeBytes(Packet.END);

			return buffer;
		} catch (Exception e) {
			throw new ParseException("packet encode() error caused by "
					+ e.getMessage());
		}
	}

	@Override
	protected void releasePacket() {
	}

}